home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Programmer's Power Pack
/
Delphi Volume 1.iso
/
s_to_z
/
wmapdemo
/
data.z
/
CURVES.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1996-03-11
|
4KB
|
112 lines
unit Curves;
interface
const
POLY_MAX = 5;{the number of points which comprise the bezier curve}
{the more points, the smoother the curve & the longer}
{ the processing time required. }
NUMBEROFSTEPS = 20;
{increase this to increase the number of}
{plotted points in any curve. the current value}
{is not a bad one for curves that do not encompass }
{more than half the screen. decreasing the value will}
{speed up the process, at the expense of curve smoothing}
type
real_array = array[0..POLY_MAX] of real; {holds the curve points}
{public functions}
procedure plotBezierCurve(n:integer;xa,ya : real_array);
{****************************************************************************}
implementation
{global variables}
Var
n: integer;
xa,ya : real_array;
Bez_Coeff : real_array;
{internal procedure to calculate the Bezier Coefficient for a point}
procedure CalcBezierCoeffs(nm1 : Integer; Var Bez_Coeff : real_array);
Var
k,j : integer;
{spot the FORTRAN programmer!!}
begin
for k := 0 to nm1 do begin
Bez_coeff[k] := 1.0;
for j := nm1 downto k + 1 do
Bez_Coeff[k] := Bez_Coeff[k] * j;
for j := nm1 - k downto 2 do
Bez_coeff[k] := Bez_coeff[k] / j;
end;
end;
function BezBlendingValue (nm1, z : integer; u :real; Bez_Coeff : real_array) :real;
Var
bv,v : real;
i : integer;
begin
bv := Bez_Coeff[z];
for i := 1 to z do
bv := bv * u;
v := 1 - u;
for i := 1 to nm1 - z do
bv := bv * v;
BezBlendingValue := bv;
end;
procedure ComputeBezPt (n : integer; xa,ya : real_Array;u : real;
Bez_Coeff :real_array; var x,y : real);
Var
k, nm1 : integer;
bv : real;
Begin
x := 0.0;
y := 0.0;
nm1 := n -1 ;
for k := 0 to nm1 do begin
bv := BezBlendingValue(nm1,k,u,Bez_Coeff);
x := x + xa[k+1] * bv;
y := y + ya[k +1] * bv;
end;
End;
{****************************************************************************}
{ procedure : plotBezierCurve }
{ inputs: }
{ n - number of xy coordinates in the curve }
{ xa, ya - xy coordinates arrays }
{ output: nice little lines on the screen!! }
{ the procedure will plot a curve who's start point is defined as the first }
{ element of the xa,ya array, passing as close as possible to the middle }
{ points of the array, and ending at the last point in the array. Each of the}
{ middle points has an effect on how close you can get to the other mid }
{ points, so having one point which is wildly different from a smooth curve }
{ will cause the curve to pass less closely to each of the mid points than }
{ may be desirable. The smoothness of the curve can be increased by raising }
{ the value of the constant NUMBEROFSTEPS or by increasing the number of points}
{****************************************************************************}
procedure plotBezierCurve(n:integer;xa,ya : real_array);
Var
i,nm1 : integer;
x1,y1,x2,y2,u : real;
begin
nm1 := n - 1 ;
CalcBezierCoeffs(nm1,Bez_Coeff);
ComputeBezPt(n,xa,ya,0.0,Bez_Coeff,x1,y1);
for i:= 1 to NUMBEROFSTEPS do begin
u := i / NUMBEROFSTEPS;
ComputeBezPt(n,xa,ya,u,Bez_Coeff,x2,y2);
{Delphi drawing stuff- you will need to USES the relavent unit to }
{avoid compile-time moans}
Form1.Image.Canvas.MoveTo(Round(x1),Round(y1));
Form1.Image.Canvas.LineTo(Round(x2),Round(y2));
x1 := x2;
y1 := y2;
end;
end;
end.